Data Types in R
Data types refer to the kind of data that can be stored and manipulated within a program. In R, the basic data types include:
-
Numeric: Represents real numbers (e.g., 2, 15.5).
-
Integer: Represents whole numbers (e.g., 2L, where L denotes an integer).
-
Character: Represents strings (e.g., “hello”, “1234”). Character must be put between “.
-
Logical: Represents Boolean values (TRUE or FALSE).
Assigning Values and Basic Operations
Assignment Operator
- The assignment operator in R is used to assign values to variables or objects in the R programming language.
- The leftwards assignment operator <-: This is the most commonly used assignment operator in R. It assigns the value on its right to the object on its left. For example, x <- 3 assigns the value 3 to the variable x.
- Alternative Assignment Operator (=) Apart from <-, R also supports the use of the = operator for assignments, similar to many other programming languages.
- However, the use of <- is preferred in R for historical and readability reasons. For example, x = 3 is valid but x <- 3 is more idiomatic to R.
Use <-
or =
for assigning values, e.g., x <- 10
or x= 10
Arithmetic operators
- In R, arithmetic operators are used to perform common mathematical operations on numbers, vectors, matrices, and arrays. Here’s an overview of the primary arithmetic operators available in R:
+
, -
, *
, /
, ^
Division (/) operator - Divides the first number or vector by the second, element-wise.
Square (^) operator - Squares the first number by the second.
Statements
Logical Operations
Includes ==
, !=
, >
, <
, >=
, <=
.
Equality: ==
checks if two values are equal.
Code
# Equality 5 == 3
x <- 5
y <- 3
x == y
Inequality: !=
checks if two values are not equal.
Code
# Inequality 5 != 3
x != y
Greater than: >
checks if the value on the left is greater than the value on the right.
Code
# Greater than 5 > 3
x > y
Less than: <
checks if the value on the left is less than the value on the right.
Greater than or equal to: >=
checks if the value on the left is greater than or equal to the value on the right.
Code
# Greater than or equal to 5 >= 3
x >= y
Less than or equal to: <=
checks if the value on the left is less than or equal to the value on the right.
Code
# Less than or equal to 5 <= 3
y <= x
Data Structures
Vectors
- Vectors are fundamental data structures that hold elements of the same type.
- They are one-dimensional arrays that can store numeric, character, or logical data.
- Assigning data to vectors in R is a basic operation, essential for data manipulation and analysis.
- The
c()
function combines values into a vector. It’s the most common method for creating vectors.
Code
# Numeric vector
age <- c(20, 21, 23)
age
Code
# Character vector
student <- c("ajay", "vijay", "jay")
student
Code
# logical vector
Pass<- c(TRUE, FALSE, TRUE)
Pass
Matrix
- A two-dimensional, rectangular collection of elements of the same type.
- All elements must be of the same data type.
- Created using the matrix() function. nrow is used to set number of rows and byrow is used to set values by rows (if TRUE) or columns (if FALSE).
Code
# Create a matrix with 2 rows and 3 columns
my_matrix <- matrix(data = c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3, byrow = TRUE)
# Print the matrix
print(my_matrix)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
Array
- Similar to matrices but can have more than two dimensions.
- Elements within an array must all be of the same data type.
- Created using the array() function. dimensions are set using dim.
Code
# Create a 3-dimensional array with dimensions 2x3x2
my_array <- array(data = c(1:24), dim = c(2, 3, 4))
# Print the array
print(my_array)
, , 1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
, , 2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
, , 3
[,1] [,2] [,3]
[1,] 13 15 17
[2,] 14 16 18
, , 4
[,1] [,2] [,3]
[1,] 19 21 23
[2,] 20 22 24